
#include "../ui.h"
#include "../app_includes.h"

#define WAVE_POINT_NUM      OSC_WAVE_SIZE
#define WAVE_VALUE_MAX      (4096)
#define WAVE_INTERVAL_BASE  (500) //nS

#define METER_NUM   (6)
typedef struct
{
    lv_obj_t *btn[2];
    lv_obj_t *edit;
}lv_select_edit_t;

static lv_obj_t *ui_screen;
static lv_obj_t *ui_Chart;
static lv_obj_t *ui_PanelMeter;
static lv_obj_t *ui_LabelMeter[METER_NUM];
static lv_obj_t *ui_Panel1;
static lv_obj_t *ui_BtnPause;
static lv_obj_t *ui_Dropdown1;
static lv_obj_t *ui_Dropdown2;
static lv_obj_t *ui_Dropdown3;
static lv_obj_t *ui_Dropdown4;
static lv_obj_t *ui_table;
static lv_obj_t *ui_Slider1;
static lv_obj_t *ui_Label1;
static lv_select_edit_t v_div;
static lv_select_edit_t t_div;

static lv_timer_t *lv_timer;
static lv_chart_series_t *ui_series_wave;
static lv_coord_t wave[WAVE_POINT_NUM];
static uint32_t interval = 1; //时间间隔倍数
static uint16_t has_show_num = 0;

static void div_init(lv_select_edit_t *edit, lv_obj_t *parent, lv_align_t align, lv_coord_t x, lv_coord_t y);
static void wave_refresh(lv_timer_t *timer);


static void btn_pause_event_click(lv_event_t * e);
static void btn_v_div_add_event_click(lv_event_t * e);
static void btn_v_div_sub_event_click(lv_event_t * e);
static void btn_t_div_add_event_click(lv_event_t * e);
static void btn_t_div_sub_event_click(lv_event_t * e);
static void btn_ch_select_event_click(lv_event_t * e);

void lv_oscilloscope_close(void)
{
    lv_timer_del(lv_timer);
}

//app应用入口
lv_obj_t *lv_oscilloscope_open(void)
{
    uint16_t i;
    ui_screen = lv_screen_create(ui_main_screen.desktop, LV_HOR_RES, LV_VER_RES);

    ui_Chart = lv_chart_create(ui_screen);
    lv_obj_set_height(ui_Chart, 280);
    lv_obj_set_width(ui_Chart, lv_pct(100));
    lv_obj_align(ui_Chart, LV_ALIGN_TOP_MID,0, UI_STATUS_BAR_HEIGHT);
    lv_chart_set_type(ui_Chart, LV_CHART_TYPE_LINE);
    lv_chart_set_point_count(ui_Chart,WAVE_POINT_NUM);//图表上点的个数
    lv_chart_set_range(ui_Chart, LV_CHART_AXIS_PRIMARY_Y, 0, WAVE_VALUE_MAX);
    lv_chart_set_div_line_count(ui_Chart, 9, 15);
    lv_chart_set_axis_tick(ui_Chart, LV_CHART_AXIS_PRIMARY_X, 8, 4, 15, 5, false, 50);
    lv_chart_set_axis_tick(ui_Chart, LV_CHART_AXIS_PRIMARY_Y, 8, 4, 9, 5, false, 50);
    lv_chart_set_axis_tick(ui_Chart, LV_CHART_AXIS_SECONDARY_Y, 10, 5, 0, 0, false, 25);
    ui_series_wave = lv_chart_add_series(ui_Chart, lv_color_hex(0x808080), LV_CHART_AXIS_PRIMARY_Y);
    lv_chart_set_ext_y_array(ui_Chart, ui_series_wave, wave);
    lv_obj_set_style_bg_color(ui_Chart, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Chart, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_Chart, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_size(ui_Chart, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_chart_set_series_color(ui_Chart, ui_series_wave, lv_color_hex(0x00ff00));

    ui_PanelMeter = lv_obj_create(ui_Chart);
    lv_obj_set_width( ui_PanelMeter, lv_pct(100));
    lv_obj_set_height( ui_PanelMeter, 140);
    lv_obj_align( ui_PanelMeter, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_clear_flag( ui_PanelMeter, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelMeter, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelMeter, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelMeter, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_PanelMeter, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);

    for(i=0;i!=3;i++) {
        ui_LabelMeter[i] = lv_label_create(ui_PanelMeter);
        lv_obj_set_width( ui_LabelMeter[i], LV_SIZE_CONTENT);
        lv_obj_set_height( ui_LabelMeter[i], LV_SIZE_CONTENT);
        lv_label_set_text(ui_LabelMeter[i],"最大值:");
        if(i==0)
            lv_obj_align( ui_LabelMeter[i], LV_ALIGN_TOP_LEFT, 300, 0);
        else
            lv_obj_align_to( ui_LabelMeter[i], ui_LabelMeter[i-1], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    }
    for(i=3;i!=6;i++) {
        ui_LabelMeter[i] = lv_label_create(ui_PanelMeter);
        lv_obj_set_width( ui_LabelMeter[i], LV_SIZE_CONTENT);
        lv_obj_set_height( ui_LabelMeter[i], LV_SIZE_CONTENT);
        lv_label_set_text(ui_LabelMeter[i],"最大值:");
        if(i==3)
            lv_obj_align( ui_LabelMeter[i], LV_ALIGN_TOP_LEFT, 0, 0);
        else
            lv_obj_align_to( ui_LabelMeter[i], ui_LabelMeter[i-1], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    }

    ui_Panel1 = lv_obj_create(ui_screen);
    lv_obj_set_width( ui_Panel1, 470);
    lv_obj_set_height( ui_Panel1, 140);
    lv_obj_set_x( ui_Panel1, 0 );
    lv_obj_set_y( ui_Panel1, 130 );
    lv_obj_align(ui_Panel1, LV_ALIGN_BOTTOM_MID,0,5);
    lv_obj_clear_flag( ui_Panel1, LV_OBJ_FLAG_SCROLLABLE );    /// Flags

    div_init(&v_div, ui_Panel1, LV_ALIGN_BOTTOM_LEFT, 40, 0);
    lv_obj_add_event_cb(v_div.btn[0], btn_v_div_add_event_click, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(v_div.btn[1], btn_v_div_sub_event_click, LV_EVENT_CLICKED, NULL);
    lv_textarea_set_text(v_div.edit,"2.00VBw");
    div_init(&t_div, ui_Panel1, LV_ALIGN_BOTTOM_LEFT, 220, 0);
    lv_obj_add_event_cb(t_div.btn[0], btn_t_div_add_event_click, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(t_div.btn[1], btn_t_div_sub_event_click, LV_EVENT_CLICKED, NULL);
    lv_textarea_set_text(t_div.edit,"20.0mS");

    ui_BtnPause = lv_btn_create(ui_Panel1);
    lv_obj_set_width( ui_BtnPause, 90);
    lv_obj_set_height( ui_BtnPause, 30);
    lv_obj_align_to( ui_BtnPause, t_div.edit, LV_ALIGN_OUT_RIGHT_MID, 50, 0);
    lv_obj_add_flag( ui_BtnPause, LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnPause, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_BtnPause, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BtnPause, lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_add_event_cb(ui_BtnPause, btn_pause_event_click, LV_EVENT_CLICKED, NULL);
    //lv_obj_set_style_bg_opa(ui_BtnPause, 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_Label1 = lv_label_create(ui_BtnPause);
    lv_obj_set_width( ui_Label1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_Label1, LV_SIZE_CONTENT);   /// 1
    lv_label_set_text(ui_Label1,"PAUSE");
    lv_obj_set_align( ui_Label1, LV_ALIGN_CENTER);
    lv_obj_set_style_text_font(ui_Label1, &ui_font_FontChinese, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_Label1, lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
    //lv_obj_set_style_bg_opa(ui_Label1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_Dropdown2 = lv_dropdown_create(ui_Panel1);
    lv_dropdown_set_options( ui_Dropdown2, "AC\nDC" );
    lv_obj_set_size( ui_Dropdown2, 90, 30);
    lv_obj_align_to( ui_Dropdown2, ui_BtnPause, LV_ALIGN_OUT_TOP_LEFT, 0, -10 );
    lv_obj_add_flag( ui_Dropdown2, LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_set_style_bg_color(ui_Dropdown2, lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_add_event_cb(ui_Dropdown2, btn_ch_select_event_click, LV_EVENT_VALUE_CHANGED, NULL);

    ui_Dropdown1 = lv_dropdown_create(ui_Panel1);
    lv_dropdown_set_options( ui_Dropdown1, "_|~\n~|_" );
    lv_obj_set_size( ui_Dropdown1, 90, 30);
    lv_obj_align_to( ui_Dropdown1, ui_Dropdown2, LV_ALIGN_OUT_TOP_LEFT, 0, -10 );
    lv_obj_add_flag( ui_Dropdown1, LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_set_style_bg_color(ui_Dropdown1, lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );

    ui_Dropdown3 = lv_dropdown_create(ui_Panel1);
    lv_dropdown_set_options( ui_Dropdown3, "x1\nx10" );
    lv_obj_set_size( ui_Dropdown3, 90, 30);
    lv_obj_align_to( ui_Dropdown3, ui_Dropdown2, LV_ALIGN_OUT_LEFT_MID, -10, 0 );
    lv_obj_add_flag( ui_Dropdown3, LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_set_style_bg_color(ui_Dropdown3, lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );

    ui_Dropdown4 = lv_dropdown_create(ui_Panel1);
    lv_dropdown_set_options( ui_Dropdown4, "Normal\nReversed" );
    lv_obj_set_size( ui_Dropdown4, 90, 30);
    lv_obj_align_to( ui_Dropdown4, ui_Dropdown1, LV_ALIGN_OUT_LEFT_MID, -10, 0 );
    lv_obj_add_flag( ui_Dropdown4, LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_set_style_bg_color(ui_Dropdown4, lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );

    ui_table = lv_table_create(ui_Panel1);
    lv_obj_align_to(ui_table, ui_Panel1, LV_ALIGN_TOP_LEFT, 0, -10);
    lv_obj_set_size(ui_table, 250, 100);
    //lv_obj_set_style_bg_color(ui_table, lv_color_hex(0xf0f0f0), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_table_set_cell_value(ui_table, 0, 0, "T1");
    lv_table_set_cell_value(ui_table, 0, 1, "200mS");
    lv_table_set_cell_value(ui_table, 0, 2, "5.00V");
    lv_table_set_cell_value(ui_table, 1, 0, "T2");
    lv_table_set_cell_value(ui_table, 1, 1, "300mS");
    lv_table_set_cell_value(ui_table, 1, 2, "4.00V");
    lv_table_set_cell_value(ui_table, 2, 0, "T2-T1");
    lv_table_set_cell_value(ui_table, 2, 1, "100mS");
    lv_table_set_cell_value(ui_table, 2, 2, "1.00V");
    lv_table_set_col_width(ui_table, 0, 60);
    lv_table_set_col_width(ui_table, 1, 90);
    lv_table_set_col_width(ui_table, 2, 90);
/*
    ui_Slider1 = lv_slider_create(ui_Chart);
    lv_obj_set_width( ui_Slider1, 228);
    lv_obj_set_height( ui_Slider1, 10);
    lv_obj_set_align( ui_Slider1, LV_ALIGN_BOTTOM_MID );
*/
    lv_timer = lv_timer_create(wave_refresh, 1, NULL);

    return ui_screen;
}

static void div_init(lv_select_edit_t *edit, lv_obj_t *parent, lv_align_t align, lv_coord_t x, lv_coord_t y)
{
    edit->edit = lv_textarea_create(parent);
    lv_obj_set_size(edit->edit, 90, LV_SIZE_CONTENT);
    lv_obj_align( edit->edit, align, x, y);
    lv_textarea_set_one_line(edit->edit,true);
    lv_obj_clear_flag(edit->edit, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(edit->edit, lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );

    edit->btn[0] = lv_btn_create(parent);
    lv_obj_set_width( edit->btn[0], 30);
    lv_obj_set_height( edit->btn[0], 30);
    lv_obj_align_to( edit->btn[0], edit->edit, LV_ALIGN_OUT_LEFT_MID, -5,0);
    lv_obj_add_flag( edit->btn[0], LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( edit->btn[0], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(edit->btn[0], lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(edit->btn[0], 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_t *ui_Label0 = lv_label_create(edit->btn[0]);
    lv_obj_set_width( ui_Label0, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_Label0, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_Label0, LV_ALIGN_CENTER );
    lv_label_set_text(ui_Label0,"+");
    lv_obj_set_style_text_font(ui_Label0, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    edit->btn[1] = lv_btn_create(parent);
    lv_obj_set_width( edit->btn[1], 30);
    lv_obj_set_height( edit->btn[1], 30);
    lv_obj_align_to( edit->btn[1], edit->edit, LV_ALIGN_OUT_RIGHT_MID, 5,0);
    lv_obj_add_flag( edit->btn[1], LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( edit->btn[1], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(edit->btn[1], lv_color_hex(0x3F3E3E), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(edit->btn[1], 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    lv_obj_t *ui_Label1 = lv_label_create(edit->btn[1]);
    lv_obj_set_width( ui_Label1, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_Label1, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_Label1, LV_ALIGN_CENTER );
    lv_label_set_text(ui_Label1,"-");
    lv_obj_set_style_text_font(ui_Label1, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

}

static void meter_cal(uint16_t num)
{
    uint16_t i;
    int32_t min, max;
    float amp = oscilloscope.meter.v_amp/(1<<oscilloscope.config.v_div);
    min = max = wave[0];
    for(i=1;i<num;i++) {
        min = MIN(min, wave[i]);
        max = MAX(max, wave[i]);
    }
    oscilloscope.meter.min = (min - oscilloscope.meter.offset)*amp;
    oscilloscope.meter.max = (max - oscilloscope.meter.offset)*amp;
    oscilloscope.meter.vpp = oscilloscope.meter.max - oscilloscope.meter.min;
    
    char str[16] = {0};
    
    for(i=0;i!=METER_NUM;i++) {
        osc_get_meter_str(str, i);
        lv_label_set_text(ui_LabelMeter[i], str);
    }
}

static void wave_refresh(lv_timer_t *timer)
{
    uint8_t triggler_flag = 0;
    uint32_t i;
    uint32_t tail_index;
    uint32_t head_index;
    uint32_t index;
    uint32_t num_data;
    uint32_t num;
    if(oscilloscope.config.pause)
        return;

    if(1==0) {//xSemaphoreTake(xSemaphoreOSC, 50) == pdTRUE) {
        //锁定数据位置
        triggler_flag = 1;
        head_index = board.adc_data.head*ADC0_BLOCK_SIZE;
    }
    else {
        head_index = board.adc_data.head*ADC0_BLOCK_SIZE;
    }
    index = head_index;
    tail_index = (board.adc_data.tail+ADC0_BLOCK_NUM-1)*ADC0_BLOCK_SIZE;
    num_data = (tail_index+ADC0_BUFF_SIZE-1-head_index)&(ADC0_BUFF_SIZE-1); //计算已采集到的数据数量
    num = num_data/interval + 1; //计算出可以存的数量
    if(num>(WAVE_POINT_NUM-has_show_num)) {//太多了存不下
        num = WAVE_POINT_NUM-has_show_num;
        if(triggler_flag==0)
            index = (head_index+((num-WAVE_POINT_NUM)*interval))&(ADC0_BUFF_SIZE-1);
    }

    for(i=0;i!=num;i++) {
        wave[has_show_num++] = board.adc_data.data[index];
        index += interval;
        index &= ADC0_BUFF_SIZE-1;
    }
    meter_cal(has_show_num);
    if(has_show_num>=WAVE_POINT_NUM)
        has_show_num = 0;
    lv_chart_refresh(ui_Chart);
    board.adc_data.head = board.adc_data.tail;
}
static void btn_pause_event_click(lv_event_t * e)
{
    lv_obj_t *t = e->target;
    oscilloscope.config.pause = lv_obj_has_state(t, LV_STATE_CHECKED);

}

static void btn_v_div_add_event_click(lv_event_t * e)
{
    oscilloscope.config.v_div++;
    if(oscilloscope.config.v_div>=OSC_V_DIV_NUM)
        oscilloscope.config.v_div = 0;
    osc_select_ch(oscilloscope.config.ch_select, oscilloscope.config.v_div);
}

static void btn_v_div_sub_event_click(lv_event_t * e)
{
    if(oscilloscope.config.v_div==0)
        oscilloscope.config.v_div = OSC_V_DIV_NUM;
    else
        oscilloscope.config.v_div--;
    osc_select_ch(oscilloscope.config.ch_select, oscilloscope.config.v_div);
}

//按照0.5, 1, 2, 5, 10, 20, 50规律递增
static void interval_cal(uint8_t div)
{
    uint8_t i;
    uint8_t a[] = {1,2,4}; //与WAVE_INTERVAL_BASE有关
    interval = a[div % 3];
    for(i=0;i!=div / 3;i++)
        interval*=10;
}

static void btn_t_div_add_event_click(lv_event_t * e)
{
    oscilloscope.config.t_div++;
    interval_cal(oscilloscope.config.t_div);
}

static void btn_t_div_sub_event_click(lv_event_t * e)
{
    if(oscilloscope.config.t_div>0) {
        oscilloscope.config.t_div--;
        interval_cal(oscilloscope.config.t_div);
    }
}


static void btn_ch_select_event_click(lv_event_t * e)
{
    lv_obj_t *t = e->target;
    uint16_t index = lv_dropdown_get_selected(t);
    osc_select_ch(index, oscilloscope.config.v_div);
}

